﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour {

    private enum GameState
    {
        start,
        playing,
        gameover
    }

    GameState gameState;

    private PlayerMove m_playerMove;
    private Attack m_attack;
    private PlayerHealth m_playerHealth;
    private Transform m_player;
    private Text m_startText;

    private void Awake()
    {
        m_player = GameObject.FindGameObjectWithTag("Player").transform;
        m_playerMove = m_player.GetComponent<PlayerMove>();
        m_attack = m_player.GetComponent<Attack>();
        m_playerHealth = m_player.GetComponent<PlayerHealth>();
        m_startText = GameObject.Find("StartText").GetComponent<Text>();
    }

    void Start ()
    {
        Init();
	}
	
	void Update ()
    {
        if (gameState == GameState.start)
        {
            if (Input.GetKeyDown(KeyCode.M))
            {
                Play();
            }
        }
        else if (gameState == GameState.playing)
        {

        }
        else if (gameState == GameState.gameover)
        {
            
        }
	}

    private void Init()
    {
        gameState = GameState.start;

        //初始化人物的 等级、生命值、位置
        m_player.position = new Vector3(15f,15f,25f);
        m_player.rotation = Quaternion.Euler(0f,180f,0f);
        m_playerHealth.Init();


        //初始化敌人

        //展现UI
        m_startText.text = "按下 “M” 开始";
    }

    public IEnumerator GameOver()
    {
        gameState = GameState.gameover;

        //禁用人物的 移动、射击、收集
        m_attack.SetShootable(false);
        m_playerMove.SetMoveable(false);

        //展现UI

        //重新开始游戏
        //Invoke("Init", 2.0f);
        yield return new WaitForSeconds(5.0f);
        SceneManager.LoadScene("01");
    }

    private void Play()
    {
        gameState = GameState.playing;

        //开启人物的 移动、射击、收集
        m_attack.SetShootable(true);
        m_playerMove.SetMoveable(true);

        //展现UI
        m_startText.text = "";
    }

}
